package org.campuscontent.ui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.model.AbstractGraph;
import org.campuscontent.util.GraphInspector;

public class Template11 extends TemplateBase implements KeyListener
{
	public static int paraID = 11;
	public JTextField textfieldReg;
	public boolean isPreviewed = false;
	public Template11(MainFrame mf)
	{
		super(mf);
		JLabel labelReg = new JLabel("Regular Expression:");
		textfieldReg = new JTextField();
		textfieldReg.setBorder(BorderFactory.createLoweredBevelBorder());

		labelReg.setBounds(20, 30, 160, 20);
		textfieldReg.setBounds(150,30,400,20);
		textfieldReg.addKeyListener(this);
		
		left.add(labelReg);
		left.add(textfieldReg);
		
	}


	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
	}

	public String getAnswer()
	{
		return "";
	}
	
	public boolean setPreview()
	{
		if (!super.setPreview()) return false;
		isPreviewed = true;
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
		return true;
	}

	public boolean exitPreview()
	{
		isPreviewed = false;
		mf.aeSplit.setDividerLocation(Config.POSITION_TO_HIDE_WRONG_AUTOMATA_EDITOR);
		textfieldReg.setEditable(true);
		super.exitPreview();
		return true;
	}

	// if the answer is correct then return
	// else show the graph of the wrong input and return the answer is wrong
	public String checkAns()
	{
		String check = "";
		String ans = textfieldReg.getText().trim();
		if (ans.length()>0)
		{
			GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
			String reNow = gi.getRExpression();
			// if answer is correct
			if (reNow.equalsIgnoreCase(ans))
			{	
				check = "Right answer!";
			}
			else  // answer is wrong
			{
				showWrongAE(ans);
				check = "wrong answer!";
			}
		}
		return check;
	}

	public void showSolution()
	{
		GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
		String reNow = gi.getRExpression();
		textfieldReg.setText(reNow);
		textfieldReg.setEditable(false);
		super.showSolution();
		mf.aeSplit.setDividerLocation(Config.POSITION_TO_HIDE_WRONG_AUTOMATA_EDITOR);

	}
	
	public boolean updateToLObjectNew()
	{

		{
			super.updateToLObjectNew();
			return true;
		}
	}
	public int getId()
	{
		return paraID;
	}
	
	public void showWrongAE(String re)
	{
		GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
		AbstractGraph ag = gi.getGraphFromRe(re);
		AutomataEditor aeWrong = new AutomataEditor(false);
		aeWrong.constructGraph(ag);
		mf.aeSplit.add(aeWrong,JSplitPane.RIGHT);
		mf.aeSplit.setDividerLocation(300);
		aeWrong.setEnable(false);
	}


	public void keyPressed(KeyEvent e) {
		mf.aeSplit.setDividerLocation(Config.POSITION_TO_HIDE_WRONG_AUTOMATA_EDITOR);
		if (isPreviewed)       // only under preview can have reaction to ENTER
		 if (e.getKeyChar() == KeyEvent.VK_ENTER)
		 {
			 checkAns();
			 String txt = textfieldReg.getText();
			 textfieldReg.setText(txt.trim());
		 }
		
	}


	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
